#include <myheader_c_plus.h>
using std::endl;
using std::cout;
class Person{
public:
    //无参构造函数
    Person()
    :_pname(new char [1]())
    ,_age(0)
    {

    }

    //构造函数
    Person(const char * pname, size_t age)
    :_pname(new char[strlen(pname) + 1]())
    ,_age(age)
    {
        strcpy(_pname, pname);
    }

    /*
     * void getName_Age(){
     *     cout << "name = " << _pname << " " << "age = " << _age << endl;
     * }
     */


    //拷贝构造函数
    Person(const Person & rhs)
    :_pname(new char[strlen(rhs._pname) + 1]())
    ,_age(rhs._age)
    {
        strcpy(_pname,  rhs._pname);
    }



    //赋值运算函数
    Person & operator=(const Person & rhs){
        if(this != &rhs){
            delete [] _pname;
            _pname = new char[strlen(rhs._pname) + 1]();
            strcpy(_pname, rhs._pname);
            _age = rhs._age;
        }
        return *this;
    }


    //析构函数
    ~Person(){
        if(_pname){
            delete [] _pname;
            _pname = nullptr;
        }
    }


    //输出成员
    void display(){
        cout << "name = " << _pname << " " << "age = " << _age << endl;
    }


private:
    char * _pname;
    size_t _age;
};

class Employee : public Person{
public:
    //无参构造
    Employee()
    :_pdepart(new char[1]())
    ,_salary(0.0)
    {
        cnt++;
    }


    //构造函数
    Employee(const char *pname, size_t age, const char * pdepart, double salary)
    :Person(pname, age)
    ,_pdepart(new char[strlen(pdepart) + 1]())
    ,_salary(salary)
    {
        cnt++;
        totalSalary += salary; 
        strcpy(_pdepart, pdepart);
    }


    //拷贝构造函数
    Employee(const Employee & rhs)
    :Person(rhs)
    ,_pdepart(new char [strlen(rhs._pdepart) + 1]())
    ,_salary(rhs._salary)
    {
        cnt++;
        totalSalary += rhs._salary;
        strcpy(_pdepart, rhs._pdepart);
    }


    //赋值运算函数
    Employee & operator=(const Employee & rhs){
        if(this != &rhs){
            totalSalary -= _salary;
            totalSalary += rhs._salary;
            Person::operator=(rhs);
            delete [] _pdepart;
            _pdepart = new char [strlen(rhs._pdepart) + 1]();
            strcpy(_pdepart, rhs._pdepart);
            _salary = rhs._salary;
        }
        return *this;
    }


    //析构函数
    ~Employee(){
        if(_pdepart){
            delete [] _pdepart;
            _pdepart = nullptr;
        }
    }


    //打印
    void display(){
        Person::display();
        cout << "depart = " << _pdepart << " " << "salary = " << _salary << endl;
        cout << "============" << endl;
    }


    //获取平均工资
    static double getAveSarlary(){
        if(cnt != 0){
            return totalSalary / cnt;
        }else{
            return 0.0;
        }
        
    }
private:
    char * _pdepart;
    double _salary;
    static size_t cnt;
    static double totalSalary;
};
size_t Employee::cnt = 0;
double Employee::totalSalary = 0.0;
int main(int argc, char *argv[])
{
    Person p("jack", 10);
    Person p2 = p;
    Person p3;
    p3 = p;
    
    Employee e("bob", 20, "info", 3000);
    e.display();
    Employee e2("john",  30, "hr", 5000);
    cout << Employee::getAveSarlary() << endl;
    e = e2;
    e.display();
    cout << Employee::getAveSarlary() << endl;
    Employee e3 = e2;
    e3.display();
    cout << Employee::getAveSarlary() << endl;
    return 0;
}

